﻿Imports BusinessRule.Business
Imports BusinessRule.BusinessFactory
Imports BusinessRule.Common
Imports log4net
Imports System.Configuration
Imports BusinessRule.IBusiness
Imports External.APICalling
Imports Camstar.WebClient.WebConstants
Imports Camstar.WebClient.FrameworkForm

Partial Class Login
    Inherits Page
    'Camstar.WebClient.FrameworkForm.FrameworkLoginForm
    Protected PageTitle As String = "Login"
    Dim adHelper As New ADHelper 'add tianFW 2017/09/14
    Dim oBusiness As uLMSUserLoginIBusiness = uMESBusinessFactory.instance.GetuLMSUserLoginObject

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)

        Dim domains As String = Camstar.WebClient.WebUtility.WebConfigUtil.GetDefaultSettingsValue("Domains")
        If String.IsNullOrEmpty(domains) Then
            DomainList.DataSource = New String() {"."}
        Else
            DomainList.DataSource = domains.Split(",")
        End If

        DomainList.DataBind()
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not IsPostBack Then
            '单点登录时传递用户名
            Dim username As String = Request.Headers("UserName")
            If String.IsNullOrWhiteSpace(username) Then
                username = Request.QueryString.Get("UserName")
            End If
            If Not String.IsNullOrWhiteSpace(username) Then
                userNameTxtBox.Text = username
                Dim dto As BusinessRule.DTO.uMES_AjaxEmployeeDto = oBusiness.GetEmployeeByName(userNameTxtBox.Text.ToLower)
                passwordTxtBox.Text = dto.strPassword
                OnLoginClicked(sender, e)
            End If
        End If

        txtCard.Focus()
    End Sub

    Protected Sub OnLoginClicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles loginButton.Click
        Dim dto As BusinessRule.DTO.uMES_AjaxEmployeeDto
        lblMessage.Text = "" 'add tianFW 2017/09/18
        Dim isCardLog = False
        If String.IsNullOrWhiteSpace(txtCard.Text) Then
            If String.IsNullOrWhiteSpace(userNameTxtBox.Text) Then
                Return
            Else
                dto = oBusiness.GetEmployeeByName(userNameTxtBox.Text.ToLower)
            End If
        Else
            isCardLog = True
            Dim strCard = Me.txtCard.Text
            Me.txtCard.Text = ""
            dto = oBusiness.GetEmployeeByCardNum(strCard)
            userNameTxtBox.Text = dto.strEmployeeName
            If Not String.IsNullOrEmpty(dto.strPassword.Trim) Then
                passwordTxtBox.Text = dto.strPassword
            End If
        End If

        ' Session.Remove("CurrentUserInfo")
        Dim strLoginInfo As String = ""
        If String.IsNullOrWhiteSpace(dto.strEmployeeName) Then
            lblMessage.Text = "用户名不存在"
            txtCard.Focus()
            Exit Sub
        End If
        If dto.strPassword = adHelper.Encrypto(passwordTxtBox.Text) Or dto.strPassword = passwordTxtBox.Text Then 'modify tianFW 2017/09/14 将验证修改为加密与非加密都可以 ’dto.strPassword = passwordTxtBox.Text 验证卡登录或单点登录
            '  GetUserSession(dto.strEmployeeName, dto.MenuName, dto.HasMaintMenu, strLoginInfo)
            strLoginInfo = "Logged In"
            If strLoginInfo = "Login Failed - invalid user name or password" Then
                lblMessage.Text = strLoginInfo
            ElseIf strLoginInfo = "Logged In" Then
                Dim para As New Dictionary(Of String, String) '存储扫卡登录
                para("EmployeeName") = dto.strEmployeeName
                para("IP") = Request.UserHostAddress.ToString()
                If para("IP") = "::1" Then '获取本地ip信息
                    Dim hosthome = System.Net.Dns.GetHostName
                    para("IP") = hosthome '服务器名
                End If
                If isCardLog = False Then 'add tianFW
                    para("IsCard") = "0"
                Else
                    para("IsCard") = "1"
                End If
                oBusiness.AddUserLoginInfo(para)
                Session("RealUserName") = dto.strEmployeeName

                Dim userAgent = Request.UserAgent
                If userAgent.Contains("MSIE") Then 'ie浏览器 add:Wangjh 20220706
                    'Response.Write("<script>window.open('/InSiteWebApplication/MainWorkForm.aspx','_top')</script>")
                    Dim url = "'/InSiteWebApplication/Home/MainWork.html?EmployeeName=" + dto.strEmployeeName + "'"
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "<script>logIn(" & url & ")</script>") 'add:Wangjh 新登录，支持竖向菜单
                Else '其他浏览器
                    Dim url = "'/InSiteWebApplication/Home/MainWork.html?EmployeeName=" + dto.strEmployeeName + "'"
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "<script>logIn(" & url & ")</script>") 'add:Wangjh 新登录，支持竖向菜单
                End If
            Else
                lblMessage.Text = strLoginInfo
            End If
        Else
            lblMessage.Text = "用户名或密码无效"
        End If
        txtCard.Focus()
    End Sub

    Private Sub GetUserSession(ByVal strName As String, ByVal strMenu As String, ByVal UseSrcMain As Boolean, ByRef strMsg As String)
        Dim d_user = strName
        Dim d_password = String.Empty
        Dim frameworkClass As Camstar.WebClient.FrameworkManager.FrameworkSession
        If Not IsNothing(Application.Contents("T_" & SessionConstants.FrameworkSession)) Then
            frameworkClass = Application.Contents("T_" & SessionConstants.FrameworkSession)
            If frameworkClass.IsLoggedIn And UseSrcMain = False Then
                Session(SessionConstants.FrameworkSession) = Application.Contents("T_" & SessionConstants.FrameworkSession)
                Session(SessionConstants.SessionID) = Application.Contents("T_" & SessionConstants.SessionID)
                Session(SessionConstants.SessionStore) = Application.Contents("T_" & SessionConstants.SessionStore)
                Session(SessionConstants.UseDefaultLanguage) = Application.Contents("T_" & SessionConstants.UseDefaultLanguage)
                Session(SessionConstants.MainMenuItems) = Application.Contents("T_" & SessionConstants.MainMenuItems)
                Session(SessionConstants.UserName) = Application.Contents("T_" & SessionConstants.UserName)
                Session(SessionConstants.Password) = Application.Contents("T_" & SessionConstants.Password)
                strMsg = "Logged In"
                Exit Sub
            Else
                Application.Contents.Remove("T_" & SessionConstants.FrameworkSession)
                Application.Contents.Remove("T_" & SessionConstants.SessionID)
                Application.Contents.Remove("T_" & SessionConstants.SessionStore)
                Application.Contents.Remove("T_" & SessionConstants.UseDefaultLanguage)
                Application.Contents.Remove("T_" & SessionConstants.MainMenuItems)
                Application.Contents.Remove("T_" & SessionConstants.UserName)
                Application.Contents.Remove("T_" & SessionConstants.Password)
            End If
        End If

        oBusiness.GetDomainUser(strMenu, GetUserCacheCount(), d_user, d_password)

        frameworkClass = New Camstar.WebClient.FrameworkManager.FrameworkSession
        frameworkClass.Login(d_user, d_password, "", DomainList.SelectedItem.Text, Application, Session, strMsg)
        Session(Camstar.WebClient.WebConstants.SessionConstants.Password) = d_password
        Application.Contents.Add("T_" & SessionConstants.FrameworkSession, frameworkClass)
        Application.Contents.Add("T_" & SessionConstants.SessionID, Session(SessionConstants.SessionID))
        Application.Contents.Add("T_" & SessionConstants.SessionStore, Session(SessionConstants.SessionStore))
        Application.Contents.Add("T_" & SessionConstants.UseDefaultLanguage, Session(SessionConstants.UseDefaultLanguage))
        Application.Contents.Add("T_" & SessionConstants.MainMenuItems, Session(SessionConstants.MainMenuItems))
        Application.Contents.Add("T_" & SessionConstants.UserName, Session(SessionConstants.UserName))
        Application.Contents.Add("T_" & SessionConstants.Password, Session(SessionConstants.Password))
    End Sub

    Private Function GetUserCacheCount() As Dictionary(Of String, Integer)
        Dim Cachelist = New Dictionary(Of String, Integer)()
        Dim count As Integer
        For Each _cache As DictionaryEntry In Cache
            If Cachelist.TryGetValue(_cache.Value.ToString, count) Then
                Cachelist(_cache.Value.ToString) = count + 1
            Else
                Cachelist.Add(_cache.Value.ToString, 1)
            End If
        Next
        Return Cachelist
    End Function

End Class

